var tab = null;
var leftAccordion = null;
var tree = null;
var tabItems = [];
$(function(){
	$("#layout").ligerLayout({ leftWidth: 220, height: '100%',heightDiff:-34,space:4, onHeightChanged: f_heightChanged,isRightCollapse: true });
	var height = $(".l-layout-center").height();
	$("#framecenter").ligerTab({
        height: height,
        showSwitchInTab : true,
        showSwitch: true,
        onAfterAddTabItem: function (tabdata)
        {
            tabItems.push(tabdata);
            //saveTabStatus();
        },
        onAfterRemoveTabItem: function (tabid)
        { 
            for (var i = 0; i < tabItems.length; i++)
            {
                var o = tabItems[i];
                if (o.tabid == tabid)
                {
                    tabItems.splice(i, 1);
                   // saveTabStatus();
                    break;
                }
            }
        },
        onReload: function (tabdata)
        {
            var tabid = tabdata.tabid;
            addFrameSkinLink(tabid);
        }
    });
	
	$("#leftAccordion").ligerAccordion({ height: height - 25, speed: null });
	
	$(".l-link").hover(function (){
        $(this).addClass("l-link-over");
    	}, function (){
        $(this).removeClass("l-link-over");
    });
	/* $("#tree").esTree({
        //url:$webroot + 'sysResource/querySysResources.do',
        onSelect: function (node)
        {
            if (!node.data.url) return;
            var tabid = $(node.target).attr("tabid");
            if (!tabid)
            {
                tabid = node.data.id;
                $(node.target).attr("tabid", tabid)
            } 
            f_addTab(tabid, node.data.name, $webroot+node.data.url);
        }
    }); */
	tab = liger.get("framecenter");
	leftAccordion = liger.get("leftAccordion");
	tree = liger.get("tree");
	$("#pageloading").hide();
	css_init();
    pages_init(); 
});
function f_heightChanged(options){  
    if (tab){
    	 tab.addHeight(options.diff);
    }			       
    if (leftAccordion && options.middleHeight - 25 > 0){
    	 leftAccordion.setHeight(options.middleHeight - 25);
    }     
}
function f_addTab(tabid, text, url)
{
    tab.addTabItem({
        tabid: tabid,
        text: text,
        url: url,
        callback: function ()
        { 
            addFrameSkinLink(tabid); 
        }
    });
}
function closeCurrentTab(tabid){
	if(!tabid){
		tabid = $("#framecenter > .l-tab-content > .l-tab-content-item:visible").attr("tabid");
	}
	if(tab){
		tab.removeTabItem(tabid);
	}
};
function addFrameSkinLink(tabid)
{
    var prevHref = getLinkPrevHref(tabid) || "";
    var skin = getQueryString("skin");
    if (!skin) return;
    skin = skin.toLowerCase();
    attachLinkToFrame(tabid, prevHref + skin_links[skin]);
}
var skin_links = {
    
    "aqua": "plugin/ligerui/skins/Aqua/css/ligerui-all.css",
    "gray": "plugin/ligerui/skins/Gray/css/all.css",
    "silvery": "plugin/ligerui/skins/Silvery/css/style.css",
    "gray2014": "plugin/ligerui/skins/Gray2014/css/all.css",
    "tab": "plugin/ligerui/skins/Tab/css/all.css"
};
function pages_init()
{
    var tabJson = $.cookie('liger-home-tab'); 
    if (tabJson)
    { 
        var tabitems = JSON2.parse(tabJson);
        for (var i = 0; tabitems && tabitems[i];i++)
        { 
            f_addTab(tabitems[i].tabid, tabitems[i].text, tabitems[i].url);
        } 
    }
}
function saveTabStatus()
{ 
    $.cookie('liger-home-tab', JSON2.stringify(tabItems));
}
function css_init()
{
    var css = $("#mylink").get(0), skin = getQueryString("skin");
    $("#skinSelect").val(skin);
    $("#skinSelect").change(function ()
    { 
        if (this.value)
        {
            location.href = ctxPath+"/index.do?skin=" + this.value;
        } else
        {
            location.href = ctxPath+"/index.do";
        }
    });

   
    if (!css || !skin) return;
    skin = skin.toLowerCase();
    $('body').addClass("body-" + skin); 
    $(css).attr("href", skin_links[skin]); 
}
function getQueryString(name)
{
    var now_url = document.location.search.slice(1), q_array = now_url.split('&');
    for (var i = 0; i < q_array.length; i++)
    {
        var v_array = q_array[i].split('=');
        if (v_array[0] == name)
        {
            return v_array[1];
        }
    }
    return false;
}
function attachLinkToFrame(iframeId, filename)
{ 
    if(!window.frames[iframeId]) return;
    var head = window.frames[iframeId].document.getElementsByTagName('head').item(0);
    var fileref = window.frames[iframeId].document.createElement("link");
    if (!fileref) return;
    fileref.setAttribute("rel", "stylesheet");
    fileref.setAttribute("type", "text/css");
    fileref.setAttribute("href", $webroot+filename);
    head.appendChild(fileref);
}
function getLinkPrevHref(iframeId)
{
    if (!window.frames[iframeId]) return;
    var head = window.frames[iframeId].document.getElementsByTagName('head').item(0);
    var links = $("link:first", head);
    for (var i = 0; links[i]; i++)
    {
        var href = $(links[i]).attr("href");
        if (href && href.toLowerCase().indexOf("ligerui") > 0)
        {
            return href.substring(0, href.toLowerCase().indexOf("plugin") );
        }
    }
}